<?php
//产品
class AgroupAction extends CommonAction {

	public function index()
	{
		$name = $this->getActionName();
		$m = M($name); // 实例化Data数据对象
		import('ORG.Util.Page');// 导入分页类
		$map = array('status'=>1);
		$count      = $m->where($map)->count();
		$Page       = new Page($count,10);
		$Page->rollPage = 20;	//最多显示20个分页
		// 进行分页数据查询 注意page方法的参数的前面部分是当前的页数, 使用 $_GET[p]获取
		$nowPage = isset($_REQUEST['p']) ? $_REQUEST['p'] : 1;
		$offect = ($nowPage-1)*$Page->listRows;

		$sql = 'select * from '.getTableName($name).' 
				where 1
				order by id desc
				limit '.$offect.','.$Page->listRows;
		$list = $m->query( $sql );
		$show = $Page->show();
		$this->assign('page',$show);
		$this->assign('list',$list);
		$this->display();
	}
	
	public function add()
	{
		if($_POST['subflag']){
            $name = $this->getActionName();
            $m = M($name);
            $m->create();
            $m->powerid=implode(',',$_POST['powerids']);
            $result = $m->add();
            if($result) {
                $this->success('操作成功！',"index");
                exit();
            }else{
                $this->error('写入错误！',"index");
                exit();
            }
        }else{
 			//============获取权限数组==============//
	    	$list=$this->power_list();
			$lists=$this->array_tree($list,0);
			$this->assign('chackboxs',$lists);
			//============获取权限数组 end==============//
			$this->display();
		}
	}

	public function edit() {
        if ($this->_get('id')) {
        	$name = $this->getActionName();
            $m = M($name);
            $condition['id'] = $this->_get('id');
            $res = $m->where($condition)->select();
            if($res){
            	$poweridarr=explode(',',$res[0][powerid]);
            	//============获取权限数组==============//
	        	$list=$this->power_list();
				$lists=$this->array_tree($list,0,$poweridarr);
				$this->assign('chackboxs',$lists);
				//============获取权限数组 end==============//
                $this->assign('vo', $res[0]); 
            }
        }else{
            $this->ajaxReturn(0, '非法进入', 0);
        }

        $this->display();
    }

    //更新
	public function update()
	{
		if($_POST['submit']=='确定'){
			$name = $this->getActionName();
            $m = M($name);
            $m->create();
			$m->powerid=implode(',',$_POST['powerids']);
	        $result = $m->save();
	        if($result) {
	            $this->success('操作成功！',"index");
	        }else{
	            $this->error('写入错误！',"index");
	        }
	    }else{
	    	$this->success('操作成功！',"index");
	    }
	}


    //权限信息
    public function power_list(){
    	$name=admin_power;
    	$mn = M($name);
		$sql = 'select id,name,p_id from '.getTableName($name).' where status = 1 order by id';
		$list = $mn->query( $sql );
		return $list;
    }


    //递归
    public function array_tree($arr,$index,$chackarr){
    	$return='';
    	if(count($poweridarr)< 0)$chackarr=array();
    	foreach ($arr  as $k=>$v){
				if($v['p_id']==$index){
					if(in_array($v[id],$chackarr))$checked='checked';
					$return.='<label><input name="powerids[]"  '.$checked.' type="checkbox" value="'.$v[id].'" />'.$v[name].'</label> ';
					$tmp=$this->array_tree($arr,$v['id'],$chackarr);
					if($tmp)$return.='<br>'.$tmp.'<br>';
					//$return[$v['id']]=$v;
					//$return[$v['id']]['info']=$this->array_tree($arr,$v['id']);
				}
		}
		return $return;
    }

}